package com.hngy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hngy.common.R;
import com.hngy.domain.Setmeal;
import com.hngy.dto.SetmealDto;
import com.hngy.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author LiKe
 * @date 2021/12/29 10:39
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @CacheEvict(value = "userSetmeal",allEntries = true)
    @PostMapping
    public R add(@RequestBody SetmealDto setmealDto){
        log.info("setmealDto = {}",setmealDto);

        setmealService.add(setmealDto);
        return R.success("添加成功！");
    }

    /**
     * 分页查询套餐
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R page(int page,int pageSize,String name){
        log.info("page = {},pageSize = {},name = {}",page,pageSize,name);

        IPage<SetmealDto> iPage = setmealService.myPage(page, pageSize, name);

        return R.success(iPage);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @CacheEvict(value = "userSetmeal",allEntries = true)
    @DeleteMapping
    public R deleteByIds(@RequestParam List<Long> ids){
        log.info("ids = {}",ids);

        setmealService.delByIds(ids);

        return R.success("删除成功！");
    }

    /**
     * 修改套餐状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R status(@PathVariable int status,@RequestParam List<Long> ids){
        log.info("status = {} , ids = {}",status,ids);

        setmealService.updateStatus(status,ids);

        return R.success("修改状态成功！");

    }

    /**
     * 根据套餐分类查询套餐
     * @param categoryId
     * @param status
     * @return
     */
    @Cacheable(value = "userSetmeal",key = "#categoryId + '_' + #status")
    @GetMapping("/list")
    public R list(Long categoryId,int status){

        LambdaQueryWrapper <Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getStatus,status);
        queryWrapper.eq(Setmeal::getCategoryId,categoryId);
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
}
